package com.yao.longhan.presenter.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yao.longhan.R;
import com.yao.longhan.model.Catalog;
import com.yao.longhan.presenter.BaseAdapter;
import com.yao.longhan.presenter.BaseHolder;
import com.yao.longhan.utils.FileUtils;

import butterknife.BindView;

/**
 * Created by heyao on 8/11/17.
 */

public class CatalogAdapter extends BaseAdapter<Catalog> {

    private int currentChapter;
    private String bookId;

    public CatalogAdapter(Context context, int currentChapter, String bookId) {
        super(context);
        this.currentChapter = currentChapter;
        this.bookId = bookId;
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_catalog, parent, false);
        return new CatalogHolder(view);
    }

    public void setCurrentChapter(int chapter) {
        currentChapter = chapter;
        notifyDataSetChanged();
    }

    class CatalogHolder extends BaseHolder<Catalog> {

        @BindView(R.id.tvTocItem)
        TextView tvChapter;

        public CatalogHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void bindData(int position, Catalog catalog) {
            tvChapter.setText(catalog.getChapName());
            Drawable drawable;
            if (currentChapter == position + 1) {
                tvChapter.setTextColor(ContextCompat.getColor(context, R.color.light_red));
                drawable = ContextCompat.getDrawable(context, R.mipmap.ic_toc_item_activated);
            } else if (FileUtils.getChapterFile(bookId, position + 1).length() > 10) {
                tvChapter.setTextColor(ContextCompat.getColor(context, R.color.light_black));
                drawable = ContextCompat.getDrawable(context, R.mipmap.ic_toc_item_download);
            } else {
                tvChapter.setTextColor(ContextCompat.getColor(context, R.color.light_black));
                drawable = ContextCompat.getDrawable(context, R.mipmap.ic_toc_item_normal);
            }
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvChapter.setCompoundDrawables(drawable, null, null, null);
        }
    }
}
